<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class AccessController extends CommonController {

	public function index() {
		$M=new \Think\Model("Admin");
		$count = $M->count();
		$page =new \Think\Page($count,15);
		$showPage = $page->show();
		$this->assign("page", $showPage);
		$this->assign("pageCount",$count);
		$list=D("Access")->adminList($page->firstRow, $page->listRows);
		foreach ($list as $key => &$value) {
			$hhh=M('auth_group')->where('id='.$value['juese'])->find();
			$value['guanli']=$hhh['title'];
		}
		$this->assign("list",$list);
		$this->display();
	}
	
	
		
	/**
	 +----------------------------------------------------------
	 * 添加管理员
	 +----------------------------------------------------------
	 */
	public function addAdmin() {
		if (IS_POST) {
			$this->checkToken();
			header('Content-Type:application/json; charset=utf-8');
			$resultt=D("Access")->addAdmin();
			if($resultt){
				$data['uid']=$resultt;
				$data['group_id']=I('post.id');
                  M('auth_group_access')->add($data);


				//$this->success('添加成功',U('Access/index'));
				$this->redirect('Access/index');
			}else{
				$this->error(D("Access")->getError());
			}
		} else {
             $data=M('auth_group')->select();

       $this->assign('data',$data);
			$this->assign('navAction','添加管理员');
			$this->display();
		}
	}
	

	/**
	 +----------------------------------------------------------
	 * 修改密码
	 +----------------------------------------------------------
	 */
	public function editPass() {
		if (IS_POST) {
			$this->checkToken();
			header('Content-Type:application/json; charset=utf-8');
			if(D("Access")->editPwd()){
				$this->success('修改成功',U('Index/welcome'));
			}else{
				$this->error(D("Access")->getError(),U('Access/editPass'));
			}
		} else {
			$this->assign('navAction','修改密码');
			$this->display();
		}
	}
	
	
	/**
	 +----------------------------------------------------------
	 * 修改 管理员
	 +----------------------------------------------------------
	 */
	public function editAdmin() {
		if (IS_POST) {
			$this->checkToken();
			header('Content-Type:application/json; charset=utf-8');
			if(D("Access")->editAdmin()){
				$this->redirect('Access/index');
				//$this->success('修改成功',U('Access/index'));
			}else{
				$this->success('未修改',U('Access/index'));
			}
		} else {
			$M = M("Admin");
			$aid = (int) $_GET['aid'];
			$info = $M->where("`aid`=" . $aid)->find();
			if (empty($info['aid'])) {
			    $this->error('不存在该管理员ID'); 
			}
			// if ($info['username'] == C('ADMIN_AUTH_KEY')) {
			// 	 $this->error('超级管理员信息不允许操作'); 
			// }
     
			$id = (int) $_GET['id'];

			 $group_data = M("auth_group")->select();
         $group_data1=M('auth_group_access')
                ->where(array('uid'=>$aid))
                ->getField('group_id');


        $this->assign("group_data1", $group_data1);
		$this->assign("group_data", $group_data);
			$this->assign("info", $info);
			$this->assign('navAction','修改管理员');
			$this->display();
		}
	}
	
	
	
	
	public function delAdmin(){

		$ids = is_numeric($_GET['aid']) ? array($_GET['aid']) : $_POST['aid'];
		$str="('".implode("','",$ids)."')";
	
		if (M("Admin")->where("aid in ".$str)->delete()) {
			
			echo json_encode(array("status"=>1,"info"=>"成功删除"));
		} else {
			echo json_encode(array("status"=>0,"info"=>"删除失败，可能是不存在该ID的记录"));
		}
	}
	
    		
}

    
  


?>